//
//  WEXUserAccountViewModel.m
//  microBlog_oc
//
//  Created by apple on 16/4/21.
//  Copyright © 2016年 Hill. All rights reserved.
//

#import "WEXUserAccountViewModel.h"
#import "WEXNetworkTools.h"
#import "WEXUserAccount.h"

static NSString *WB_APP_KEY = @"1336075890";

static NSString *WB_REDIRECT_URI = @"http://www.baidu.com/";

static NSString *WB_APP_SECRET = @"f535e5d4033d624ccad804392593a7db";

@interface WEXUserAccountViewModel()

@property (nonatomic, strong) NSString *accessToken;

@end

@implementation WEXUserAccountViewModel

-(NSString *)accessToken{
    
    return self.account.access_token;
}


-(instancetype)init{
    
    if ( self = [super init]) {
        
//        从沙盒中获取账号
        self.account = [self accountSandBox];
        
    }
    
    return self;
    
}

- (BOOL)isLogin{
    
    if (self.accessToken == nil) {
        
        return NO;
    }
    
    NSDate *date = [[NSDate alloc] init];
    
    if ([date compare:self.account.expiresDate] == NSOrderedAscending) {
        
        
        return YES;
    }
    
    return NO;
    
    
}

# pragma mark -
# pragma mark 实现单例
+(instancetype)sharedViewMoel {
    
    static WEXUserAccountViewModel *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        instance = [[WEXUserAccountViewModel alloc] init];
        
    });
    
    return instance;
    
}

- (void)loadAccessToken:(NSString *)code withFinished:(void (^)(BOOL isSuccess))finished{
    
    NSString *urlSring = @"https://api.weibo.com/oauth2/access_token";
    
    NSDictionary *parames = @{
                             @"client_id": WB_APP_KEY,
                             @"client_secret": WB_APP_SECRET,
                             @"grant_type": @"authorization_code",
                             @"code": code,
                             @"redirect_uri": WB_REDIRECT_URI
                             
                             };
    
    
    [[WEXNetworkTools sharedTools] request:WEXRequestMethodPost urlString:urlSring parameters:parames finished:^(NSDictionary *responseObject, NSError *error) {
       
        
        if (error != nil) {
            finished(NO);
            NSLog(@"%@",error);
            
            return ;
        }
//            字典转模型
            WEXUserAccount *account = [WEXUserAccount userAccountWithDict:responseObject];
            
            
            NSLog(@"%@",account);
        
        
        [self loadUserInfoWith:account withFinished:finished];
        
    }];
    
    
}

# pragma mark -
# pragma mark 下载用户信息
- (void)loadUserInfoWith:(WEXUserAccount *)account withFinished:(void(^)(BOOL isSuccess))finished{
    
//    接口地址
    NSString *urlString = @"https://api.weibo.com/2/users/show.json";
    
//    参数
    NSDictionary *parames = @{@"access_token": account.access_token,
                              @"uid": account.uid};
//    网络请求数据
    [[WEXNetworkTools sharedTools] request:WEXRequestMethodGet urlString:urlString parameters:parames finished:^(NSDictionary *responseObject, NSError *error) {
        
        NSLog(@"error= %@",error);
        
        if (error != nil) {
            
            finished(NO);
            
            return ;
        }
        
        
//        给账号信息赋值
        account.name = responseObject[@"name"];
        
//        设置账号头像
        account.avatar_large = responseObject[@"avatar_large"];
        
        self.account = account;
        
        [self saveAccount:account];
        
        NSLog(@"--- account-----%@", self.account);
        finished(YES);
    }];
    
}

# pragma mark -
# pragma mark 存入沙盒
- (void)saveAccount:(WEXUserAccount *)account{
    
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"account.archive"];
    
    [NSKeyedArchiver archiveRootObject:account toFile:path];
    
}

# pragma mark -
# pragma mark 从沙盒中取出
- (WEXUserAccount *)accountSandBox {
    
     NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"account.archive"];
    
    WEXUserAccount *account = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    
    return account;
}

@end
